using BasicScript.Component;
using Game.Const;
using Game.Core;
using Game.Main.Model;
using Game.Main.View;
using Game.Manager;
using Game.XH;
using SimpleJSON;
using UnityEngine;

namespace Game.Component
{
	public class CarItem : BaseItem
	{
		private float SPEED = 360f;

		private Transform color1;

		private Transform color2;

		private SpriteRenderer[] color1s;

		private SpriteRenderer[] color2s;

		private int leftTyreId;

		private int rightTyreId;

		private Transform leftTyre;

		private Transform rightTyre;

		private SpriteAssets tyreAssets;

		private float angle;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
			color1 = frontMask.Find("Color1");
			color1s = InitColor(color1);
			InitComponentTrigger(color1.gameObject, ItemComponentTrigger.Type.CarColor);
			SetColor1(data.bodyColor);
			color2 = frontMask.Find("Color2");
			if (color2 != null)
			{
				color2s = InitColor(color2);
				InitComponentTrigger(color2.gameObject, ItemComponentTrigger.Type.CarColor);
				SetColor2(data.color);
				Transform transform = frontMask.Find("Color3");
				if (transform != null)
				{
					InitComponentTrigger(transform.gameObject, ItemComponentTrigger.Type.CarColor);
				}
				color2.SetLocalZ(-1E-05f);
			}
			Transform transform2 = frontMask.Find("Body");
			int childCount = transform2.childCount;
			for (int i = 0; i < childCount; i++)
			{
				Transform child = transform2.GetChild(i);
				child.SetLocalZ(1E-05f * child.localPosition.z);
			}
			tyreAssets = model.GetComponent<SpriteAssets>();
			leftTyre = transform2.Find("LeftTyre");
			rightTyre = transform2.Find("RightTyre");
			InitComponentTrigger(leftTyre.gameObject, ItemComponentTrigger.Type.LeftTyre);
			InitComponentTrigger(rightTyre.gameObject, ItemComponentTrigger.Type.RightTyre);
			SetLeftTyre(data.intParam1);
			SetRightTyre(data.intParam2);
		}

		private SpriteRenderer[] InitColor(Transform color)
		{
			if (color == null)
			{
				return null;
			}
			int childCount = color.childCount;
			SpriteRenderer[] array = new SpriteRenderer[childCount];
			for (int i = 0; i < childCount; i++)
			{
				Transform child = color.GetChild(i);
				child.SetLocalZ(1E-05f * child.localPosition.z);
				array[i] = child.GetComponent<SpriteRenderer>();
			}
			return array;
		}

		public void ChangeTyre(bool isLeft, int id)
		{
			ItemData itemData = ((!isLeft) ? Singleton<SceneModel>.Instance.GetItemDataByTyreId(rightTyreId) : Singleton<SceneModel>.Instance.GetItemDataByTyreId(leftTyreId));
			if (itemData != null)
			{
				Singleton<AudioManager>.Instance.PlaySound("sound_explode");
				Vector3 vector;
				BaseItem baseItem;
				if (isLeft)
				{
					vector = new Vector3(leftTyre.position.x, base.transform.position.y + 0.01f, base.transform.position.z);
					baseItem = Singleton<Scene>.Instance.CreateItem(itemData.id, leftTyre.position, 0);
				}
				else
				{
					vector = new Vector3(rightTyre.position.x, base.transform.position.y + 0.01f, base.transform.position.z);
					baseItem = Singleton<Scene>.Instance.CreateItem(itemData.id, rightTyre.position, 0);
				}
				baseItem.DropDown(vector + new Vector3(0f, 0.01f, 0f));
				Singleton<EffectManager>.Instance.ShowEffect(PathConst.EFFECT + "efc_explode_1", vector + new Vector3(0f, 0.5f, -0.0004f), 1f);
				Singleton<EffectManager>.Instance.ShowEffect(PathConst.EFFECT + "efc_blink_1", vector + new Vector3(0f, 0.5f, -0.0004f), 3f);
			}
			if (isLeft)
			{
				SetLeftTyre(id);
			}
			else
			{
				SetRightTyre(id);
			}
			Singleton<AudioManager>.Instance.PlaySound("sound_embed");
		}

		public override void TouchDown(TouchEventData touchEventData)
		{
			base.TouchDown(touchEventData);
		}

		protected override void PlayDragSound()
		{
			if (base.itemData.model.Contains("1"))
			{
				Singleton<AudioManager>.Instance.PlayAudio("sound_car_1", true, GetAudioPlayer());
			}
			else
			{
				Singleton<AudioManager>.Instance.PlayAudio("sound_car_2", true, GetAudioPlayer());
			}
		}

		protected override void StopDragSound()
		{
			RemoveAudioPlayer();
		}

		public override void TouchMove(TouchEventData touchEventData)
		{
			base.TouchMove(touchEventData);
			angle -= Time.deltaTime * SPEED;
			SetTyreRotate(angle);
		}

		private void SetTyreRotate(float angle)
		{
			if (archiveData.dirX > 0)
			{
				leftTyre.eulerAngles = new Vector3(0f, 0f, angle);
				rightTyre.eulerAngles = new Vector3(0f, 0f, angle);
			}
			else
			{
				leftTyre.eulerAngles = new Vector3(0f, 0f, 0f - angle);
				rightTyre.eulerAngles = new Vector3(0f, 0f, 0f - angle);
			}
		}

		public void ChangeColor(Transform trans, Color32 color)
		{
			if (trans == color1)
			{
				SetColor1(color);
			}
			else if (color2 != null)
			{
				SetColor2(color);
			}
		}

		public void SetColor1(Color32 color)
		{
			for (int i = 0; i < color1s.Length; i++)
			{
				color1s[i].color = color;
			}
		}

		public void SetColor2(Color32 color)
		{
			for (int i = 0; i < color2s.Length; i++)
			{
				color2s[i].color = color;
			}
		}

		public Color32 GetColor(Transform comp)
		{
			if (comp == color1)
			{
				if (color1s != null)
				{
					return color1s[0].color;
				}
			}
			else if (comp == color2 && color2s != null)
			{
				return color2s[0].color;
			}
			return new Color32(byte.MaxValue, byte.MaxValue, byte.MaxValue, byte.MaxValue);
		}

		private void SetLeftTyre(int id)
		{
			leftTyreId = id;
			leftTyre.GetComponent<SpriteRenderer>().sprite = GetSprite(id);
		}

		private void SetRightTyre(int id)
		{
			rightTyreId = id;
			rightTyre.GetComponent<SpriteRenderer>().sprite = GetSprite(id);
		}

		private Sprite GetSprite(int id)
		{
			string value = "tyre_" + id;
			for (int i = 0; i < tyreAssets.data.sprites.Count; i++)
			{
				if (tyreAssets.data.sprites[i].name.Equals(value))
				{
					return tyreAssets.data.sprites[i];
				}
			}
			return null;
		}

		public override void PackData(JSONObject obj)
		{
			base.PackData(obj);
			obj["59"] = leftTyreId;
			obj["60"] = rightTyreId;
			obj["27"] = ((Color32)color1s[0].color).r;
			obj["28"] = ((Color32)color1s[0].color).g;
			obj["29"] = ((Color32)color1s[0].color).b;
			if (color2s != null)
			{
				obj["30"] = ((Color32)color2s[0].color).r;
				obj["31"] = ((Color32)color2s[0].color).g;
				obj["32"] = ((Color32)color2s[0].color).b;
			}
		}

		private void InitComponentTrigger(GameObject obj, ItemComponentTrigger.Type type)
		{
			obj.layer = 12;
			ItemComponentTrigger itemComponentTrigger = obj.AddComponent<ItemComponentTrigger>();
			itemComponentTrigger.Init(this, type, null, null, null);
		}
	}
}
